//
//  BKLeftTableViewModel.m
//  FastBee
//
//  Created by mac on 16/9/11.
//  Copyright © 2016年 xbk. All rights reserved.
//

#import "BKLeftTableViewModel.h"
#import "BKNetWorkTools.h"

@implementation BKLeftTableViewModel

- (void)setValue:(id)value forUndefinedKey:(NSString *)key{

    if ([key isEqualToString:@"id"]) {
        
        self.myID = value;
        return;
    }
    //[super setValue:value forKey:key];
}

+ (instancetype)leftModelWithDic:(NSDictionary *)dic{

    BKLeftTableViewModel *model = [[BKLeftTableViewModel alloc]init];
    [model setValuesForKeysWithDictionary:dic];
    return model;
}


+ (void)downloadDataWithUrl:(NSString *)url successBlock:(void(^)(NSArray*arr,NSDictionary *dic))successBlock failBlock:(void (^)(NSError *error))failBlock{
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:@"5" forKey:@"call"];

    [[BKNetWorkTools shareNetWorkTools]POST:url parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *responseObject) {
        
       // NSLog(@"%@",responseObject);
        
        NSDictionary *dic = [responseObject objectForKey:@"data"];
        NSArray *arr = dic[@"categories"];
        
        NSDictionary *rightDic = dic[@"products"];
        
        NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:arr.count];
        
        for (NSDictionary *dic in arr) {
            
            BKLeftTableViewModel *model = [BKLeftTableViewModel leftModelWithDic:dic];
            
            [arrM addObject:model];
        }
        
        if (successBlock) {
            
            successBlock(arrM.copy,rightDic);
        }
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
        if (failBlock) {
            
            failBlock(error);
        }
    }];
}

@end


/**
 *  responseObject = @{@"data":@{ @"product":@{@"a152":@[],@"a136":@[]} @"category":@[]}}
 
    @{ @"product":@{@"a152":@[],@"a136":@[]}
    @{ @"category":@[]}
 */

